package com.zide.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zide.common.core.domain.CommonResult;
import com.zide.common.core.domain.entity.SysMenu;
import com.zide.common.core.domain.entity.SysUser;
import com.zide.common.utils.SecurityUtils;
import com.zide.system.domain.user.vo.result.SystemUserResultVO;
import com.zide.system.service.ISysUserService;
import com.zide.system.service.SysLoginService;
import com.zide.framework.web.service.impl.SysPermissionService;
import com.zide.framework.web.service.ISysMenuService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author zide
 */
@RequestMapping
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Resource
    private ISysUserService sysUserService;
    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public CommonResult getInfo() {
        Long userId = SecurityUtils.getUserId();
        SystemUserResultVO resultVO = sysUserService.selectUserById(userId);
        SysUser user = BeanUtil.copyProperties(resultVO, SysUser.class);
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        CommonResult ajax = CommonResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    @GetMapping("getRouters")
    public CommonResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return CommonResult.success(menuService.buildMenus(menus));
    }
}
